package com.dcjt.firehotcarpad.model

import android.app.Activity
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.aries.library.fast.manager.LoggerManager
import com.aries.library.fast.retrofit.FastObserver
import com.aries.library.fast.util.ToastUtil
import com.dcjt.firecommand.entity.*
import com.dcjt.firehotcarpad.entity.BaseEntity
import com.dcjt.firehotcarpad.entity.BaseListEntity
import com.dcjt.firehotcarpad.entity.DisasterInfoEntity
import com.dcjt.firehotcarpad.entity.SearchHistoryEntity
import com.dcjt.firehotcarpad.manager.DataManager
import com.dcjt.firehotcarpad.retrofit1.repository.ApiRepository1
import com.google.gson.Gson
import com.trello.rxlifecycle2.android.ActivityEvent
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity
import kotlin.collections.HashMap

class DisasterListViewModel(app: Application) : AndroidViewModel(app) {
    val requestStatusLiveData: MutableLiveData<BaseEntity<BaseListEntity<DisasterInfoEntity>>> = MutableLiveData()
    val baseBean = BaseEntity<BaseListEntity<DisasterInfoEntity>>()

    fun getDisasterList(
        params: HashMap<String, Any>,
        mContext: Activity
    ){

       var ajlx = params["ajlx"] as String?
       var ajlxdm = params["ajlxdm"]as String?
       var ajdj = params["ajdj"]as String?
       var ajdjdm = params["ajdjdm"]as String?
       var keyword = params["keyword"]as String?

        if (ajlx?.length != 0 ||ajlxdm?.length !=0 ||ajdj?.length !=0 || ajdjdm?.length !=0 || keyword?.length !=0 ){
            var historyEntity = SearchHistoryEntity(ajlx as String?,
                ajlxdm as String?, ajdj as String?, ajdjdm as String?, keyword as String?
            )
            DataManager.getInstance().saveSearchHistroy(historyEntity)
        }

        ApiRepository1.getInstance().getDisasterList( params)
            .compose((mContext as RxAppCompatActivity).bindUntilEvent(ActivityEvent.DESTROY))
            .subscribe(object : FastObserver<BaseListEntity<DisasterInfoEntity>>() {


                override fun _onError(errorRes: Int, errorCode: Int, e: Throwable) {
                    super._onError(errorRes, errorCode, e)
                    baseBean.code =  BaseEntity.err
                    baseBean.data = null
                    requestStatusLiveData.value = baseBean

                }

                override fun _onNext(entity: BaseListEntity<DisasterInfoEntity>) {
                    LoggerManager.d(Gson().toJson(entity))

                    baseBean.code = BaseEntity.ok
                    baseBean.data = entity
                    requestStatusLiveData.value = baseBean
                }

            })
    }

    fun getTJ_DisasterList(mContext: Activity){
        ApiRepository1.getInstance().getIntelligentRecommendationDisasterList( "")
            .compose((mContext as RxAppCompatActivity).bindUntilEvent(ActivityEvent.DESTROY))
            .subscribe(object : FastObserver<BaseListEntity<DisasterInfoEntity>>() {


                override fun _onError(errorRes: Int, errorCode: Int, e: Throwable) {
                    super._onError(errorRes, errorCode, e)
                    baseBean.code =  BaseEntity.err
                    baseBean.data = null
                    requestStatusLiveData.value = baseBean

                }

                override fun _onNext(entity: BaseListEntity<DisasterInfoEntity>) {

                    baseBean.code = BaseEntity.ok
                    baseBean.data = entity
                    requestStatusLiveData.value = baseBean

                }

            })
    }
}